const axios = require('axios')
class XhslinkParser {
  constructor() {
    this.headers = {
      'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.124 Safari/537.36',
      // 'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8',
      // 'Accept-Language': 'zh-CN,zh;q=0.8,zh-TW;q=0.7,zh-HK;q=0.5,en-US;q=0.3,en;q=0.2'
    }
  }
  async parse(url, timeout) {
    try {
      const response = await axios.get(url, {
        timeout,
        headers: this.headers
      })
      const html = response.data
      const candidates = html.match(/https?:\/\/[^"]+\.(mp4|m3u8)([^"\s>]*)/gi);
      if (candidates && candidates.length > 0) {
        const mediaUrl = candidates[0];
        return { data: { url: mediaUrl } }
      } else {
        return { code: 404, msg: "未找到媒体URL" };
      }
    } catch (e) {
      return { code: 500, msg: `服务器错误：${e.message || "未知错误"}` };
    }
  }
}

module.exports = XhslinkParser;